package com.nurun.cacheimageloader;

import java.net.ContentHandler;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.google.android.filecache.FileResponseCache;
import com.google.android.imageloader.BitmapContentHandler;
import com.google.android.imageloader.ImageLoader;
import com.google.android.imageloader.ImageLoader.BindResult;
import com.google.android.imageloader.ImageLoader.Callback;
import com.nurun.cacheimageloader.view.CrossFadingImageView;

public class NurunImageLoader {

    private ImageLoader oldImageLoader;
    private VolleyImageLoader volleyImageLoader;
    
    public enum ImageLoaderType {
        OLD, VOLLEY
    }

    public NurunImageLoader(RequestQueue requestQueue) {
        this.volleyImageLoader = new VolleyImageLoader(requestQueue);
    }
    
    public NurunImageLoader(Context applicationContext) {
     // Load images using a BitmapContentHandler
        // and cache the image data in the file cache.
        ContentHandler contentHandler = null;

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            contentHandler = new CacheContentHandler(applicationContext);
        } else {
            // Install the file cache (if it is not already installed)
            MyFileResponseCache.install(applicationContext);
            contentHandler = FileResponseCache.capture(new BitmapContentHandler(), null);
        }
        this.oldImageLoader = new ImageLoader(ImageLoader.DEFAULT_TASK_LIMIT, null, contentHandler, null, ImageLoader.DEFAULT_CACHE_SIZE,
                null);
    }

    public void bindWithFadeIn(ImageView view, String url, final int errorImageResId, int loadingImageResId) {
        if (oldImageLoader == null) {
            volleyImageLoader.get(url, view, loadingImageResId, errorImageResId);
        }
        else {
            bindWithFadeIn(oldImageLoader, view, url, errorImageResId, loadingImageResId);
        }
    }
    
    private void bindWithFadeIn(ImageLoader imageLoader, ImageView view, String url, final int errorImageResId, int loadingImageResId) {

        if (view == null || TextUtils.isEmpty(url)) {
            return;
        }
        final long duration = 500;
        BindResult bind = imageLoader.bind(view, url.toString(), new Callback() {

            @Override
            public void onImageLoaded(ImageView view, String url) {
                animateWithFadeIn(duration, view);
            }

            @Override
            public void onImageError(ImageView view, String url, Throwable error) {
                view.setImageResource(errorImageResId);
                animateWithFadeIn(duration, view);
            }
        });
        switch (bind) {
        case ERROR:
            view.setImageResource(errorImageResId);
        case OK:
            animateWithFadeIn(duration, view);
            break;
        case LOADING:
            //view.setImageResource(loadingImageResId);
        default:
            break;
        }
    }

    private static void animateWithFadeIn(final long duration, ImageView view) {
        // view.animate().alpha(1f).setDuration(duration).setListener(null);
        // Next line is bug fix for Android 3.x
        if (view.getAlpha() == 0f) {
            ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).setDuration(duration).start();
        }
    }

    public static void bindWithCrossFade(ImageLoader imageLoader, final CrossFadingImageView view, String url, final int errorImageResId) {
        if (view == null || TextUtils.isEmpty(url)) {
            return;
        }
        if (view.isNotSameUrl(url)) {
            view.setUrl(url);
            BindResult bind = imageLoader.bind(view.getNewView(), url, new Callback() {

                @Override
                public void onImageLoaded(ImageView newView, String url) {
                    view.crossFade();
                }

                @Override
                public void onImageError(ImageView newView, String url, Throwable error) {
                    view.setUrl("");
                    newView.setImageResource(errorImageResId);
                    view.crossFade();
                }
            });
            if (bind == BindResult.OK || bind == BindResult.ERROR) {
                view.crossFade();
            }
        }
    }
}
